<?php
if(!isset($_SESSION)) 
{ 
	session_start(); 
} 
include_once '../value/value_kr.php';
//$zipfile = new zipfile();
	if(isset($_SESSION['checkSave'])){
		
		/** Include path **/
		set_include_path(get_include_path() . PATH_SEPARATOR . 'ClassesExcel/');
		
		/** PHPExcel */
		require_once 'PHPExcel.php';
		
		/** PHPExcel_RichText */
		require_once 'PHPExcel/RichText.php';
		
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->setActiveSheetIndex(0);
		
		$styleArray = array(
		'font' => array(
			'bold' => true,
			'size' => 11,
		),
		'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		'fill' => array(
			'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
			'rotation' => 90,
			'startcolor' => array(
				'argb' => 'FFA0A0A0',
			),
			'endcolor' => array(
				'argb' => 'FFFFFFFF',
			),
		),
	);
	
	/*$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);
	//$objPHPExcel->getActiveSheet()->mergeCells('A2:L2');
	$objPHPExcel->getActiveSheet()->getStyle('A2')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->setCellValue('A2', $l_working_spreadsheet);
	
	$objPHPExcel->getActiveSheet()->freezePane('A5');
	$objPHPExcel->getActiveSheet()->getRowDimension('4')->setRowHeight(20);
	$objPHPExcel->getActiveSheet()->getStyle('A4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('A4', $l_employee_name);
	
	$objPHPExcel->getActiveSheet()->getStyle('B4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('B4', $l_code);
	
	$objPHPExcel->getActiveSheet()->getStyle('C4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('C4', $l_thu);
	
	$objPHPExcel->getActiveSheet()->getStyle('D4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('D4', $l_date);
	
	$objPHPExcel->getActiveSheet()->getStyle('E4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('E4', $l_timecheckin);
	
	$objPHPExcel->getActiveSheet()->getStyle('F4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('F4', $l_timecheckout);
	
	$objPHPExcel->getActiveSheet()->getStyle('G4')->applyFromArray($styleArray);
	//$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('G4', $l_late);
	
	$objPHPExcel->getActiveSheet()->getStyle('H4')->applyFromArray($styleArray);
	//$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('H4', $l_comebackearly);
	
	$objPHPExcel->getActiveSheet()->getStyle('I4')->applyFromArray($styleArray);
	//$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('I4', $l_half_dayoff);
	
	$objPHPExcel->getActiveSheet()->getStyle('J4')->applyFromArray($styleArray);
	//$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('J4', $l_all_dayoff);
	
	$objPHPExcel->getActiveSheet()->getStyle('K4')->applyFromArray($styleArray);
	//$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('K4', $l_overtime);
	
	$objPHPExcel->getActiveSheet()->getStyle('L4')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('L4', $l_note);*/
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(12);
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(25);
	
	$objPHPExcel->getActiveSheet()->setCellValue('A1', "No");
	$objPHPExcel->getActiveSheet()->setCellValue('B1', $l_e_date);
	$objPHPExcel->getActiveSheet()->setCellValue('C1', $l_e_time);
	$objPHPExcel->getActiveSheet()->setCellValue('D1', $l_s_canums);
	$objPHPExcel->getActiveSheet()->setCellValue('E1', $l_s_way);
	$objPHPExcel->getActiveSheet()->setCellValue('F1', $l_cameraname);
		$filename = ".xls";
		if(isset($_REQUEST['searchvalue'])){
			$filename = '_'.$_REQUEST['searchvalue'].$filename;
		}
		$currentDate = date("Y-d-m");
		
		$mylistcarnum = $_SESSION['mylistcarnum'];
		$index = 1;
		foreach ($_SESSION['checkSave'] as $key => $value) {
			$index+= 1;
			$row = $mylistcarnum[$key];
			$pos_t = strpos($row[2], " ");
			$date_t = substr($row[2], 0, $pos_t);
			$time_t = substr($row[2], $pos_t);
	        //array($row[id],$row[name],$row[car_datetime],$row[carnumber],$row[host],$row[filename],$row[isReg]);
	        
			$objPHPExcel->getActiveSheet()->getRowDimension($key)->setRowHeight(20);
		
			$objPHPExcel->getActiveSheet()->setCellValue('A'.$index, $row[0]);
			$objPHPExcel->getActiveSheet()->setCellValue('B'.$index, $date_t);
			$objPHPExcel->getActiveSheet()->setCellValue('C'.$index, $time_t);
			$objPHPExcel->getActiveSheet()->setCellValue('D'.$index, $row[3]);
			$objPHPExcel->getActiveSheet()->setCellValue('E'.$index, $row[6]);
			$objPHPExcel->getActiveSheet()->setCellValue('F'.$index, $row[1]);
			
		}
		unset($_SESSION['checkSave']);
		
		include 'PHPExcel/IOFactory.php';
	
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
		header("Cache-Control: no-store, no-cache, must-revalidate");
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache");
		header("Content-type: application/vnd.ms-excel");
		header('Content-Disposition: attachment;filename="' . $currentDate.$filename);
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
		
	}
?>